<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/5/28
 * Time: 10:30
 * 租户认证
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Tenant  extends Base {
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['uuu.user_username|uuu.user_phone'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($xq_sh_status)&&$xq_sh_status!="")
            {
                $map['a.xq_sh_status'] = $xq_sh_status;
            }
            if(isset($start) && $start != '' && isset($end) && $end != ''){
                $map['xq_apply_time'] = ['between', [$start, $end]];
            }
            if(isset($start) && $start != '' && isset($end) && $end == ''){
                $map['xq_apply_time'] = ['> time', $start];
            }
            if(isset($start) && $start == '' && isset($end) && $end != ''){
                $map['xq_apply_time'] = ['< time', $end];
            }
            $map['xq_role'] = 1;
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('xq_property')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.xq_user_id','left')
                ->join('up_xq ux','ux.xq_id =a.xq_id','left')
//                ->join('up_xq_block uxb','uxb.block_id =a.xq_block_id','left')
//                ->join('up_xq_unit uxn','uxn.unit_id =a.xq_unit_id','left')
//                ->join('up_xq_room uxr','uxr.room_id =a.xq_room_id','left')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('xq_property')->alias('a')
                ->where($map)
                ->join('up_u_user uuu','uuu.user_id = a.xq_user_id','left')
                ->join('up_xq ux','ux.xq_id =a.xq_id','left')
//                ->join('up_xq_block uxb','uxb.block_id =a.xq_block_id','left')
//                ->join('up_xq_unit uxn','uxn.unit_id =a.xq_unit_id','left')
//                ->join('up_xq_room uxr','uxr.room_id =a.xq_room_id','left')
                ->page($NowPage,$limits)
                ->field('a.*,uuu.user_username,uuu.user_phone,ux.xq_name')
//                ->field('a.*,uuu.user_username,uuu.user_phone,ux.xq_name,uxb.block_name,uxn.unit_name,uxr.room_name')
                ->order('a.xq_apply_time desc')
                ->select();
            foreach ($lists as $key=>&$vo){
                $vo['name'] = $vo['xq_name'].'-'.$vo['xq_house_num'];
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }

        return $this->fetch();
    }
    /**
     * 审核通过
     * @return \think\response\Json
     */
    public function allow(){
        $role_id = input('id');
        $update = [
            'xq_sh_status' => 2,
            'xq_pass_time' => date('Y-m-d H:i:s')
        ];
        $info = Db::table('up_xq_property')->where('xq_m_id', $role_id)->find();
        if($info['house_id']==''){
            return json(['code' => 100, 'msg' => '不存在该房产不能审核通过']);
        }
        $res = Db::table('up_xq_property')->where('xq_m_id', $role_id)->update($update);
        $aa = Db::table('up_u_user')->where('user_id', $info['xq_user_id'])->find();
        if($aa['user_role']!=1){
            Db::table('up_u_user')->where('user_id', $info['xq_user_id'])->setField('user_role',1);
        }
        if($res){
//            //TODO:推送
            $list = [];
            $v = (string)$info['xq_user_id'];
            array_push($list, $v);
            $code=2;
            $str = array('msg'=>"您的租户认证已通过",'type'=>3);
            $str1 = "您的租户认证已通过";
            try{
                $this->sendJpush(1, $list, $str1, $str,$code);
            }catch(\Exception $e){

            }
            return json(['code' => 200, 'msg' => '审核成功']);
        }else{
            return json(['code' => 100, 'msg' => '审核失败']);
        }
    }

    /**
     * 审核驳回
     * @return mixed|\think\response\Json
     */
    public function refuse(){
        if(Request::instance()->isPost()){
            $id = input('id');
            $role_false_reason = input('role_false_reason');
//            $length = mb_strlen($role_false_reason, 'UTF8');
//            if($length > 30){
//                return json(['code' => 100, 'msg' => '长度不可超过30个字']);
//            }
            $update = [
                'xq_sh_status' => 3,
                'xq_nopass_reason' => $role_false_reason,
                'xq_pass_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_xq_property')->where('xq_m_id', $id)->update($update);
            $res1 = Db::table('up_xq_property')->where('xq_m_id', $id)->find();
            if($res){
                $list = [];
                $v = (string)$res1['xq_user_id'];
                array_push($list, $v);
                $code=2;
                $str = array('msg'=>$role_false_reason,'type'=>10);
                $str1 = "您的租户认证未通过:".($role_false_reason);
                try{
                    $this->sendJpush(1, $list, $str1, $str,$code);
                }catch(\Exception $e){

                }
                return json(['code' => 200, 'msg' => '审核成功']);
            }else{
                return json(['code' => 100, 'msg' => '审核失败']);
            }
        }
        $id = input('xq_m_id');
        $this->assign('id', $id);
        return $this->fetch('');
    }
}